<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>刷棒棒糖</title>
<link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<style>
.container {
  margin-top: 20px;
}
.log {
  max-height: 200px;
  overflow-y: scroll;
}
</style>
</head>
<body>
  <div class="container">
    <h1 class="text-center">登录</h1>
    <form style="width:300px;" class="center-block">
      <div class="form-group">
        <label for="username" class="sr-only">用户名</label>
        <input type="email" class="form-control" id="username" placeholder="用户名">
      </div>
      <div class="form-group">
        <label for="password" class="sr-only">密码</label>
        <input type="password" class="form-control" id="password" placeholder="密码">
      </div>
      <div class="text-center">
        <button type="button" class="btn btn-primary" onclick="login()">登录</button>
        <div id="check_info" class="alert alert-info sr-only" role="alert">验证登录中...</div>
      </div>
    </form>
  </div>
  <script>
  // 重命名 Electron 提供的 require
  window.nodeRequire = require;
  delete window.require;
  delete window.exports;
  delete window.module;
  </script>
  <script type="text/javascript" src="node_modules/jquery/jquery.min.js"></script>
  <script type="text/javascript" src="node_modules/bootstrap/dist/js/bootstrap.min.js"></script>
  <script type="text/javascript" src="node_modules/lowdb/dist/lowdb.min.js"></script>
  <script type="text/javascript" src="node_modules/lodash/lodash.min.js"></script>
  <script type="text/javascript" src="node_modules/dot/doT.min.js"></script>
  <script>
  const BrowserWindow = nodeRequire('electron').remote.BrowserWindow;
  var low = nodeRequire('lowdb');

  // $('#check_info').removeClass('sr-only');
  let login_info = low('db.json').get('login_info').value();
  let username = login_info.username;
  let password = login_info.password;
  let url = 'http://battleofballs.duapp.com/index.php/Admin/ProxyLogin/login';
  $.post(url, {username: username, password: password}, function(result) {
    console.log(result);
    if(result.status == 'ok') {
      console.log('验证登录成功');
      low('db.json').get('login_info').assign({username: username, password: password, remain_days: result.remain_days}).value();
      if(result.remain_days <= 0) {
        alert('用户已过期');
      } else {
        nodeRequire('electron').remote.getCurrentWindow().loadURL(`file://${__dirname}/index.html`);
      }
    } else if(result.status == 'err') {
      console.log('登录失败');
    }
  });

  function login() {
    let username = $('#username').val();
    let password = $('#password').val();
    let url = 'http://battleofballs.duapp.com/index.php/Admin/ProxyLogin/login';
    $.post(url, {username: username, password: password}, function(result) {
      if(result.status == 'ok') {
        console.log('登录成功');
        low('db.json').get('login_info').assign({username: username, password: password, remain_days: result.remain_days}).value();
        if(result.remain_days <= 0) {
          alert('用户已过期');
        } else {
          nodeRequire('electron').remote.getCurrentWindow().loadURL(`file://${__dirname}/index.html`);
        }
      } else if(result.status == 'err') {
        console.log('登录失败');
      }
    });
  }
  </script>
</body>

</html>
